package com.cdplife.ci.modular.system.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.cdplife.ci.core.common.annotion.BussinessLog;
import com.cdplife.ci.core.common.annotion.FormToken;
import com.cdplife.ci.core.common.annotion.Permission;
import com.cdplife.ci.core.common.constant.dictmap.PolicyPlanDict;
import com.cdplife.ci.core.common.exception.BizExceptionEnum;
import com.cdplife.ci.core.shiro.ShiroKit;
import com.cdplife.ci.modular.system.warpper.PolicyPlanWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.cdplife.ci.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.cdplife.ci.modular.system.model.PolicyPlan;
import com.cdplife.ci.modular.system.service.IPolicyPlanService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-11-07 14:27:28
 */
@Controller
@RequestMapping("/policyPlan")
public class PolicyPlanController extends BaseController {

    private String PREFIX = "/system/policyPlan/";

    @Autowired
    private IPolicyPlanService policyPlanService;

    /**
     * 跳转到首页
     */
    @RequestMapping("/{policyId}")
    public String index(@PathVariable Integer policyId, Model model) {
        model.addAttribute("policyId",policyId);
        return PREFIX + "policyPlan.html";
    }

    /**
     * 跳转到添加
     */
    @FormToken(save=true)
    @RequestMapping("/policyPlan_add/{policyId}")
    public String policyPlanAdd(@PathVariable Integer policyId, Model model) {
        model.addAttribute("policyId",policyId);
        return PREFIX + "policyPlan_add.html";
    }

    /**
     * 跳转到修改
     */
    @FormToken(save=true)
    @RequestMapping("/policyPlan_update/{policyPlanId}/{status}")
    public String policyPlanUpdate(@PathVariable Integer policyPlanId,@PathVariable Integer status, Model model) {
        Map<String, Object> data =  policyPlanService.getPolicyPlanInfo(policyPlanId);
        model.addAttribute("item",data.get("policyPlan"));
        if(status ==2 ){
            model.addAttribute("planInsuranceKinds",data.get("planInsuranceKinds"));
        }
        model.addAttribute("status",status);
        LogObjectHolder.me().set(data.get("policyPlan"));
        return PREFIX + "policyPlan_edit.html";
    }

    /**
     * 跳转到选择险种页面
     */
    @FormToken(save=true)
    @RequestMapping("/add_insurance_kind/{policyPlanId}")
    public String policyPlanUpdate(@PathVariable Integer policyPlanId, Model model) {
        Map<String, Object> data =  policyPlanService.getInsuranceKindList(policyPlanId);
        model.addAttribute("policyPlan",data.get("policyPlan"));
        model.addAttribute("insuranceKinds",data.get("insuranceKind"));
        return PREFIX + "add_insurance_kind.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list/{policyId}")
    @ResponseBody
    public Object list(@PathVariable("policyId") Integer policyId) {
        List<Map<String, Object>> result = policyPlanService.selectPolicyPlanList(policyId);
        return new PolicyPlanWarpper(result).wrap();
    }

    /**
     * 新增
     */
    @FormToken(remove=true)
    @BussinessLog(value = "添加保单计划", key = "policyId,policyPlanName,policyPlanCode", dict = PolicyPlanDict.class)
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(PolicyPlan policyPlan) {
        return policyPlanService.addPolicyPlan(policyPlan);
    }


    /**
     * 删除
     */
    @FormToken(remove=true)
    @BussinessLog(value = "删除保单计划", key = "policyPlanId", dict = PolicyPlanDict.class)
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer policyPlanId) {
        return policyPlanService.deletePolicyPlan(policyPlanId);
    }

    /**
     * 修改
     */
    @FormToken(remove=true)
    @BussinessLog(value = "修改保单计划", key = "policyId,policyPlanName,policyPlanCode", dict = PolicyPlanDict.class)
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(PolicyPlan policyPlan) {
        return policyPlanService.updatePolicyPlan(policyPlan);
    }
    /**
     * 保存计划险种
     */
    @FormToken(remove=true)
    @BussinessLog(value = "修改保单计划", key = "id", dict = PolicyPlanDict.class)
    @RequestMapping(value = "/updateInsuranceKind")
    @ResponseBody
    public Object update(@RequestParam Integer id,@RequestParam String data) {
        return policyPlanService.saveInsuranceKind(id, data);
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{policyPlanId}")
    @ResponseBody
    public Object detail(@PathVariable("policyPlanId") Integer policyPlanId) {
        return policyPlanService.selectById(policyPlanId);
    }
}
